﻿using XB.Core.MemoryStore.MemoryStore;
using XB.Core.MemoryStore.TimeSchduler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XB.Core.MemoryStore
{
    public class MemoryStoreManager
    {
        private static Dictionary<string, IMemoryStore> _stores = new Dictionary<string, IMemoryStore>();
        private static TimedTaskScheduler _scheduler = new TimedTaskScheduler("MemoryStoreManager");

        public static void Register(IMemoryStore memoryStore, string key = null)
        {
            _stores.Add(key ?? memoryStore.GetType().ToString(), memoryStore);
            _scheduler.RegisterTask(memoryStore);
        }

        public static T Get<T>(string key = null)
        {
            return (T)_stores[key ?? typeof(T).ToString()];
        }

        public void Start()
        {
            _scheduler.EnsureRunning();
        }
    }
}
